package com.aspire.boc.mms.wmf.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.Status;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EhcacheUtil {
	public static final String RESOURCE_CACHE = "resourceCache";
	public static final String ACTION_CACHE = "actionCache";
	public static final String FIELD_CACHE = "fieldCache";
	public static final String CONFIG_CACHE = "configCache";
	public static CacheManager manager;

	static Log log = LogFactory.getLog(EhcacheUtil.class);

	public static Object getValue(String cacheName, Object key) {
		Cache cache = getCache(cacheName);
		if (cache != null) {
			try {
				Element elem = cache.get(key);
				if ((elem != null) && (!cache.isExpired(elem)))
					return elem.getValue();
			} catch (Exception e) {
				log.error(
						"Get cache(" + cacheName + ") of " + key + " failed.",
						e);
			}
		}
		return null;
	}

	public static synchronized void putValue(String cacheName, Object key,
			Object value) {
		Cache cache = getCache(cacheName);
		if (cache != null)
			try {
				cache.remove(key);
				Element elem = new Element(key, value);
				cache.put(elem);
			} catch (Exception e) {
				e.printStackTrace();
				log.error(
						"put cache(" + cacheName + ") of " + key + " failed.",
						e);
			}
	}

	public static synchronized void remove(String cacheName, Object key) {
		Cache cache = getCache(cacheName);
		if (cache != null)
			try {
				cache.remove(key);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove cache(" + cacheName + ") of " + key
						+ " failed.", e);
			}
	}

	public static synchronized void removeAll(String cacheName) {
		Cache cache = getCache(cacheName);
		if (cache != null)
			try {
				cache.removeAll();
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove All cache in " + cacheName + "  failed.", e);
			}
	}

	public static synchronized void destroy(String cacheName) {
		manager.removeCache(cacheName);
	}

	public static synchronized void destroyAll() {
		manager.removalAll();
	}

	public static synchronized boolean cacheExists(String cacheName) {
		return manager.cacheExists(cacheName);
	}

	public static Cache getCache(String cacheName) throws IllegalStateException {
		return manager.getCache(cacheName);
	}

	public static void addCache(Cache c) throws IllegalStateException {
		manager.addCache(c);
	}

	public static boolean isKeyInCache(String cacheName, Object key)
			throws IllegalStateException {
		Cache c = manager.getCache(cacheName);
		return c != null ? c.isKeyInCache(key) : false;
	}

	public static Element getElement(String cacheName, Object key)
			throws IllegalStateException, CacheException {
		Cache cache = getCache(cacheName);
		return cache.get(key);
	}

	public static void shutdown() {
		if ((manager != null)
				&& (manager.getStatus().equals(Status.STATUS_ALIVE)))
			manager.shutdown();
	}


	static {
		try {
			manager = CacheManager.getInstance();
			if (manager == null)
				manager = CacheManager.create();
		} catch (CacheException e) {
			e.printStackTrace();
			log.error("Initialize cache manager failed.", e);
		}
	}
}
